package com.neiquan.backstage.meeting_room.web.action;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.BackendErrorCode;
import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Paging;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.meeting_room.MeetingRoomService;
import com.neiquan.backstage.pub.bsc.dao.po.AdminUser;
import com.neiquan.backstage.pub.bsc.dao.po.MeetingRoom;
/**
 * 会议室控制层
 * @author linjiang
 */
@Controller
@RequestMapping(value = "meetingRoomAction", produces = { "text/json;charset=UTF-8" })
public class MeetingRoomAction {
	private static Class<?> poc = MeetingRoom.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private MeetingRoomService meetingRoomService;
	/**
	 * 查询详情
	 * @param pageIndex
	 * @param pageSize
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryDetail")
	public String queryDetail(int id) {
		MeetingRoom entityM = meetingRoomService.queryById(id);
		return CommonResp.buildSuccessResp(entityM).toJsonString();
	}
	/**
	 * 分区查询
	 * @param pageIndex
	 * @param pageSize
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("list")
	public String list(int pageIndex, int pageSize, HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Criterion criterion = Restrictions.eq("memberId", loginedUser.getMemberId());
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("meetingRoomId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
	/**
	 * 分区查询
	 * @param pageIndex
	 * @param pageSize
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByMemberId")
	public String queryByMemberId(HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<MeetingRoom> listM = meetingRoomService.queryByMemberId(memberId);
		return CommonResp.buildSuccessResp(listM).toJsonString();
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "remove")
	public String remove(Integer id) {
		MeetingRoom toDelete = meetingRoomService.queryById(id);
		if (toDelete == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		extraSpringHibernateTemplate.getHibernateTemplate().delete(toDelete);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	/**
	 * 修改
	 * @param id
	 * @param name
	 * @param remark
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "edit")
	public String edit(Integer id,String name, String remark) {
		MeetingRoom toUpdate = meetingRoomService.queryById(id);
		if (toUpdate == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		toUpdate.setName(name);
		toUpdate.setRemark(remark);
		extraSpringHibernateTemplate.getHibernateTemplate().update(toUpdate);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 添加
	 * @param name
	 * @param remark
	 * @param session
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value = "add")
	public String add(String name, String remark , HttpSession session) throws ParseException {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		MeetingRoom toSave = new MeetingRoom();
		if(meetingRoomService.queryByName(memberId, name)!=null){
			return CommonResp.buildErrorResp(BackendErrorCode.already_exist).toJsonString();
			
		}
		toSave.setName(name);
		toSave.setMemberId(memberId);
		toSave.setRemark(remark);
		extraSpringHibernateTemplate.getHibernateTemplate().save(toSave);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	
}
